package com.pedro.streamer.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.pedro.streamer.R;
import com.pedro.streamer.model.AudioItem;
import com.pedro.streamer.utils.RecyclerViewSpacesItemDecoration;
import com.tencent.mmkv.MMKV;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class AIRadioChooseAdapter extends RecyclerView.Adapter<AIRadioChooseAdapter.TopTabViewHolder> {

    public interface OnChooseClick {
        public void onClick(int requestCode);
    }

    private static final String TAG = "com.bmit.engineermode";

    private Context mContext;



    private AIRadioListAdapter aiBgmListAdapter;
    private AiRadioSupportListAdapter aiRadioSupportListAdapter;
    private GridLayoutManager layoutManager;
    private List<AudioItem> audioItemList;



    private OnChooseClick onChooseClick;

    public AIRadioChooseAdapter(Context mContext) {
        this.mContext = mContext;
        this.aiBgmListAdapter = new AIRadioListAdapter(mContext);
        this.aiRadioSupportListAdapter = new AiRadioSupportListAdapter(mContext);
        this.audioItemList = new ArrayList<>();
    }

    public void setOnChooseClick(OnChooseClick onChooseClick) {
        this.onChooseClick = onChooseClick;
    }

    public AIRadioListAdapter getAiBgmListAdapter() {
        return aiBgmListAdapter;
    }

    public AiRadioSupportListAdapter getAiRadioSupportListAdapter() {
        return aiRadioSupportListAdapter;
    }

    @Override
    public TopTabViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.radio_choose_layout, parent, false);

        return new TopTabViewHolder(view);
    }

    static class TopTabViewHolder extends RecyclerView.ViewHolder {


        TextView textView;
        RecyclerView recyclerView;
        TextView choose_text;


        public TopTabViewHolder(View view) {
            //父类构造函数
            super(view);
            textView = view.findViewById(R.id.title_text);
            recyclerView = view.findViewById(R.id.recycler_view);
            choose_text = view.findViewById(R.id.choose_text);

        }

    }

    public void updateAudioList(List<AudioItem> audioItemList, int code) {

        if (code == 100) {
            aiBgmListAdapter.setAudioItemList(audioItemList);
        } else {
            aiRadioSupportListAdapter.setAudioItemList(audioItemList);
        }

    }

    @Override
    public void onBindViewHolder(@NonNull TopTabViewHolder holder, int position) {

        Log.d(TAG, "onBindViewHolder: ");
        
        HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();
        stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.TOP_DECORATION, 1);
        holder.recyclerView.addItemDecoration(new RecyclerViewSpacesItemDecoration(stringIntegerHashMap));
        layoutManager = new GridLayoutManager(mContext, 1);
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        holder.recyclerView.setLayoutManager(layoutManager);


        if (position == 0){
            Log.d(TAG, "onBindViewHolder: 0");
            List<AudioItem> audioItems = new ArrayList<>();
            try {
                Gson gson = new Gson();
                Type type = new TypeToken<List<AudioItem>>(){}.getType();
                audioItems = gson.fromJson(MMKV.defaultMMKV().decodeString("bgm", ""), type);

                Log.d(TAG, "bgm数组" + audioItems.toString());
            }catch (Exception e){
                Log.i(TAG, "解析wifi数据错误");
                e.printStackTrace();
            }
            if (audioItems == null) {
                audioItems = new ArrayList<>();
            }
            aiBgmListAdapter.setAudioItemList(audioItems);
            holder.recyclerView.setAdapter(aiBgmListAdapter);
            holder.textView.setText("背景音乐");
            holder.choose_text.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.d(TAG, "onClick: ");

                    if (onChooseClick != null) {
                        onChooseClick.onClick(100);
                    }
                }
            });
        }else {
            Log.d(TAG, "onBindViewHolder: 1");

            List<AudioItem> audioItems = new ArrayList<>();
            try {
                Gson gson = new Gson();
                Type type = new TypeToken<List<AudioItem>>(){}.getType();
                audioItems = gson.fromJson(MMKV.defaultMMKV().decodeString("support", ""), type);
            }catch (Exception e){
                Log.i(TAG, "解析wifi数据错误");
                e.printStackTrace();
            }
            if (audioItems == null) {
                audioItems = new ArrayList<>();
            }
            aiRadioSupportListAdapter.setAudioItemList(audioItems);
            holder.recyclerView.setAdapter(aiRadioSupportListAdapter);
            holder.textView.setText("助播音频");
            holder.choose_text.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.d(TAG, "onClick: ");
                    if (onChooseClick != null) {
                        onChooseClick.onClick(101);
                    }
                }
            });
        }

    }




    @Override
    public int getItemCount() {

        return 2;
    }
}
